<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Charge_elements_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function get_engine(){
      $sql = "SELECT * FROM ubs_compute_engine_v";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function insert_chg_element($data){
        $this->db->query("call ubs_charge_ins(?,?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function update_chg_element($data){
        $this->db->query("call ubs_charge_upd(?,?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function insert_chg_option($data){
        $this->db->query("call ubs_charge_option_ins(?,?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
        return $que->result();
    }
    public function update_chg_option($data){
        $this->db->query("call ubs_charge_option_upd(?,?,?,?,@err,@msg,?)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function get_option_chg($id){
        $this->db->where('charge_id', $id);
        $query = $this->db->get('ubs_chg_element_option');
        return $query->result();
    }
    public function get_chg_element($id){
        $this->db->where('charge_id', $id);
        $query = $this->db->get('ubs_chg_elements');
        return $query->result();
    }
    public function countArr($arr,$query){
      $counter =0;
      foreach ($arr as $key ) {
          $counter++;
      }
      $arr[$counter] = $query;
      return $arr; 
    }
    public function search_chg_element($code,$name,$eng){
      $sql;$arg1="";$arg2="";$arg3="";
      $arr = array();

        $setter = 0;

        if($code != ""){
          $arg1 = "uce.charge_code LIKE '".$code."'"; $setter = 1;
          $arr = $this->countArr($arr,$arg1);
        }
        if($name != ""){
         $arg2 = "uce.charge_name LIKE '".$name."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg2);
        }
        if($eng != ""){
         $arg3 = "uce.compute_engine LIKE '".$eng."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg3);
        }
        if ($setter == 1){
        $que = implode(" AND ",$arr);
          $sql = "SELECT * FROM ubs_chg_elements uce INNER JOIN (SELECT * FROM app_lookup_values alv WHERE alv.lookup_type = 'UBS_COMPUTE_ENGINE') lu ON lu.lookup_code = uce.compute_engine WHERE ".$que ." "; 
        }else{
          $sql = "SELECT * FROM ubs_chg_elements uce INNER JOIN (SELECT * FROM app_lookup_values alv WHERE alv.lookup_type = 'UBS_COMPUTE_ENGINE') lu ON lu.lookup_code = uce.compute_engine WHERE uce.charge_code LIKE '".$code."'"; 
        }
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }
    
}